package com.gage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gage.commons.result.PageInfo;
import com.gage.commons.result.Tree;
import com.gage.commons.utils.StringUtils;
import com.gage.mapper.BsDepartmentMapper;
import com.gage.mapper.BsDepartmentUserMapper;
import com.gage.model.BsDepartment;
import com.gage.model.BsDepartmentUser;
import com.gage.service.IBsDepartmentService;
@Service
public class BsDepartmentServiceImpl extends ServiceImpl<BsDepartmentMapper, BsDepartment> implements IBsDepartmentService{
	@Autowired
	private BsDepartmentMapper departmentMapper;
	@Autowired
	private BsDepartmentUserMapper departmentUserMapper;
	@Override
	public String selectDepartmentTree(Integer grade,Integer parentid) {
		List<Tree> trees = new ArrayList<Tree>();
        List<BsDepartment> departments =departmentMapper.selectDepartmentTreeByGradeOrPid(grade,parentid);
        for (BsDepartment department : departments) {
            Tree tree = new Tree();
            tree.setId(Long.parseLong(department.getId().toString()));
            tree.setText(department.getDepartmentName());
            tree.setIconCls(department.getIcon());
            if(StringUtils.isEmpty(grade)&&StringUtils.isEmpty(parentid)){
            	tree.setPid(Long.valueOf(department.getParentid()));
            	if (department.getParentid()==0) {
            		int count = departmentMapper.getCountChild(department.getId());
	 				if (count > 0){
	 					tree.setState("closed");
	 				} else{
	 					tree.setState("open");
	 				}
				}
            }
            trees.add(tree);
        }
        JSONArray array = JSONArray.fromObject(trees);
		return array.toString();
	}

	@Override
	public PageInfo selectDataGrid(PageInfo pageInfo) {
		 Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
	        page.setOrderByField(pageInfo.getSort());
	        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
	        List<Map<String, Object>> list = departmentMapper.selectDepartmentByKeyword(page, pageInfo.getCondition());
	        pageInfo.setRows(list);
	        pageInfo.setTotal(page.getTotal());
	        return pageInfo;
	}

	@Override
	public List<BsDepartment> selectDepartmentList(Integer grade,
			Integer parentid) {
		return departmentMapper.selectDepartmentTreeByGradeOrPid(grade,parentid);
	}

	@Override
	public BsDepartment selectDepartmentById(Integer id) {
		// TODO Auto-generated method stub
		return departmentMapper.selectDepartmentById(id);
	}

	@Override
	public int insertDepartmentUser(BsDepartmentUser bsDepartmentUser) {
		// TODO Auto-generated method stub
		return departmentMapper.insertDepartmentUser(bsDepartmentUser);
	}

	@Override
	public PageInfo getDepartmentUserByDepartmentId(PageInfo pageInfo) {
		 Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
	        List<Map<String, Object>> list = departmentMapper.getDepartmentUserByDepartmentId(page, pageInfo.getCondition());
	        pageInfo.setRows(list);
	        pageInfo.setTotal(page.getTotal());		
	        return pageInfo;
	}

	@Override
	public int deleteByDepartmentIdAndUid(Long departmentId, Long userId) {
		return departmentUserMapper.deleteByDepartmentIdAndUid(departmentId, userId);
	}

	@Override
	public PageInfo selectAssignUserPage(PageInfo pageInfo) {
		 Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
	        page.setOrderByField(pageInfo.getSort());
	        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
	        List<Map<String, Object>> list = departmentMapper.selectAssignUserPage(page, pageInfo.getCondition());
	        pageInfo.setRows(list);
	        pageInfo.setTotal(page.getTotal());
	        return pageInfo;
	}

	@Override
	public int getAssignUserCount(Long departmentId) {
		return departmentUserMapper.getAssignUserCount(departmentId);
	}
}
